package scanner.rule.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import scanner.rule.pojo.rule;

import com.opensymphony.xwork2.ActionSupport;

public class Rule extends ActionSupport {
	
	private List<rule> rule= new ArrayList<rule>();
	
	public List<rule> getRule() {
		return rule;
	}

	public void setRule(List<rule> rule) {
		this.rule = rule;
	}

	public String execute() throws Exception{
		readfile("D:\\KuGou");
		
		
		return "success";
	}
	
	 /**
     * 读取某个文件夹下的所有文件
     */
    public boolean readfile(String filepath) throws FileNotFoundException, IOException {
            try {

                    File file = new File(filepath);
                    if (!file.isDirectory()) {
                            System.out.println("文件");
                            System.out.println("path=" + file.getPath());
                            System.out.println("absolutepath=" + file.getAbsolutePath());
                            System.out.println("name=" + file.getName());

                    } else if (file.isDirectory()) {
                            System.out.println("文件夹");
                            String[] filelist = file.list();
                            for (int i = 0; i < filelist.length; i++) {
                                    File readfile = new File(filepath + "\\" + filelist[i]);
                                    if (!readfile.isDirectory()) {
                                            System.out.println("path=" + readfile.getPath());
                                            System.out.println("absolutepath="
                                                            + readfile.getAbsolutePath());
                                            System.out.println("name=" + readfile.getName());
                                            rule r = new rule();
                                            r.setId(i);
                                            r.setName(readfile.getName());
                                            r.setPath(readfile.getAbsolutePath());
                                            rule.add(r);
                                    } else if (readfile.isDirectory()) {
                                            readfile(filepath + "\\" + filelist[i]);
                                    }
                            }

                    }

            } catch (FileNotFoundException e) {
                    System.out.println("readfile()   Exception:" + e.getMessage());
            }
            return true;
    }
}
